package com.qs.eggs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class PocAdapter extends BaseAdapter {
    List<Map<String, Object>> giflist=new ArrayList<>();
    private Context context;


    public PocAdapter(Context context, List<Map<String, Object>> giflist) {
        super();
        this.context = context;
        this.giflist = giflist;
    }
    @Override
    public int getCount() {
        return giflist.size();
    }


    @Override
    public Object getItem(int position) {
        return giflist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.tehui, null);
            holder.name= (TextView) convertView.findViewById(R.id.name);
            holder.price= (TextView) convertView.findViewById(R.id.jiage);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView);
            holder.cart = (ImageView) convertView.findViewById(R.id.cart);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(context).load(Constants.URL_BASE+"productimages"+"/"+giflist.get(position).get("autoname"))
                .into(holder.image);
        Picasso.with(context).load(R.mipmap.icon_cart)
                .into(holder.cart);
        holder.name.setText(giflist.get(position).get("proname").toString());
        holder.price.setText("¥"+giflist.get(position).get("saleprice")+"");
        holder.cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCart(giflist.get(position).get("proid").toString(),giflist.get(position).get("proname").toString(),giflist.get(position).get("saleprice").toString(),
                        giflist.get(position).get("type").toString(),giflist.get(position).get("specification").toString());
            }
        });
        return convertView;
    }

    public class ViewHolder {

        TextView name,price;
      ImageView image,cart;
    }

    //添加购物车
    private void addCart(final String proid, final String proname, final String price, final String type, final String specification){
        HttpApi.cheakIsLogin(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if(s.equals("\"false\"")){
                    LoginActivity.show(context);
                }else {
                    HashMap<String,String> map=new HashMap<String, String>();
                    map.put("proid",proid);
                    map.put("proname",proname);
                    map.put("price",price);
                    map.put("count","1");
                    map.put("type",type);
                    map.put("specification",specification);
                    Gson gson=new Gson();
                    String str=gson.toJson(map);
                    OkGo.post(Constants.URL_BASE+"shoppingcart?action=addShoppingCart")
                            .params("data",str)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    if (s.equals("\"true\"")){
                                        Toast.makeText(context,"加入购物车成功",Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(context,"加入购物车失败",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });

                }
            }
        });
    }
}
